package previous.Test_0318;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with Intellij IDEA
 * Description:
 * User: 星辰
 */
public class Solution {
    //矩阵
    public int[][] updateMatrix(int[][] mat) {
           int m=mat.length;
           int n=mat[0].length;
           int[][] vis=new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                vis[i][j]=-1;
            }
        }
        Queue<int[]> q=new LinkedList<>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if(mat[i][j]==0){
                    q.add(new int[]{i,j});
                    vis[i][j]=0;
                }
            }
        }
        int[] dx={0,0,1,-1};
        int[] dy={1,-1,0,0};
        int step=0;
        while(!q.isEmpty()){
                int[] t=q.poll();
                int a=t[0];
                int b=t[1];
                for (int j = 0; j < 4; j++) {
                    int x=dx[j]+a;
                    int y=dy[j]+b;
                    if(x>=0 && x<m && y>=0 && y<n && vis[x][y]==-1){
                        q.add(new int[]{x,y});
                        vis[x][y]=vis[a][b]+1;
                    }
                }
        }
        return vis;
    }
    //飞地的数量
    boolean[][] vis;
    int m,n;
    public int numEnclaves(int[][] grid) {
           m=grid.length;
           n=grid[0].length;
           Queue<int[]> q=new LinkedList<>();
           vis=new boolean[m][n];
       //遍历第一行和最后一行
        //(0,n-1)(m-1,n-1)
        for (int i = 0; i < n; i++) {
            if(grid[0][i]==1){
                bfs(grid,0,i);
            }
        }
        for (int i = 0; i <n ; i++) {
            if(grid[m-1][i]==1){
                bfs(grid,m-1,i);
            }
        }
        //第一列和最后一列
        for (int i = 0; i < m; i++) {
            if(grid[i][0]==1){
                bfs(grid,i,0);
            }
        }
        //复制的时候要注意下标
        for (int i = 0; i < m; i++) {
            if(grid[i][n-1]==1){
                bfs(grid,i,n-1);
            }
        }
        int count=0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if(grid[i][j]==1 && !vis[i][j]){
                    count++;
                }
            }
        }
        return count;
    }
    public void bfs(int[][] grid,int i,int j){
        Queue<int[]> q=new LinkedList<>();
        q.add(new int[]{i,j});
        vis[i][j]=true;
        while(!q.isEmpty()){
            int[] t=q.poll();
            int a=t[0];
            int b=t[1];
            int[] dx={0,0,1,-1};
            int[] dy={1,-1,0,0};
            for (int k = 0; k < 4; k++) {
                int x=dx[k]+a;
                int y=dy[k]+b;
                //复制的时候要注意下标
                if(x>=0 && x<m && y>=0 && y<n && !vis[x][y] && grid[x][y]==1){
                    grid[x][y]=0;
                    vis[x][y]=true;
                    q.add(new int[]{x,y});
                }
            }
        }
    }

}
